//
//  WYCTranslationController.m
//  初试
//
//  Created by 中企互联 on 2017/10/16.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCTranslationController.h"
#import "WYCDTViewCell.h"
#import "WYCHeaderFooterView.h"
#import "WYCTransLationModel.h"
#import "WYCTransDetailModel.h"

@interface WYCTranslationController ()<WYCHeaderFooterViewDelegate>

/** data数组*/
@property(nonatomic,strong)NSMutableArray * dataArray;

@property(nonatomic,assign)BOOL Display;
/** details数组*/
@property(nonatomic,strong)NSMutableArray *detailsArray;

/** 显示LABEL*/
@property(nonatomic,strong)UILabel * showlabel;
@end

@implementation WYCTranslationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //请求
    [self setupRequster];
    
    [self setupTableView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupRequster)];
    
    
    self.dataArray = [[NSMutableArray alloc] init];
    
    self.detailsArray = [[NSMutableArray alloc] init];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma make ---------->>请求
- (void)setupRequster{
    
    [self.dataArray removeAllObjects];
    [self.detailsArray removeAllObjects];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardId"] = self.cardId;
    [HttpTool POST:creditcardbilllist params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"responseObject----%@",responseObject);
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = responseObject[@"result"];
            NSArray * dataArray = dict[@"data"];
            for (NSDictionary * dic in dataArray) {
                
                WYCTransLationModel * taranLationModel = [WYCTransLationModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:taranLationModel];
                
                
                NSArray * detailsArray = dic[@"details"];
                NSMutableArray * arr = [[NSMutableArray alloc] init];
                
                for (NSDictionary * detailsDic in detailsArray) {
                    WYCTransDetailModel * DetailModel = [WYCTransDetailModel mj_objectWithKeyValues:detailsDic];
                    [arr addObject:DetailModel];
                }
                taranLationModel.dataArray = arr;
            }
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        if (self.dataArray.count == 0) {
            //            self.tableView.hidden = YES;
            [self addView];
            
        }else{
            
            self.showlabel.hidden = YES;
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)addView{
    UILabel * showlabel = [[UILabel alloc] init];
    showlabel.text = @"暂无账单明细";
    showlabel.textAlignment = NSTextAlignmentCenter;
    showlabel.frame = CGRectMake(0, 300 * px + (NavHeight), ScreenWidth, 60* px);
    showlabel.textColor = [UIColor grayColor];
    showlabel.font = [UIFont systemFontOfSize:SmallFont];
    [self.view addSubview:showlabel];
    self.showlabel = showlabel;
    
}
- (void)setupTableView
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = WYCTitilesViewY;
    self.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    self.tableView.backgroundColor = WYCGlobalBg;
    self.tableView.sectionHeaderHeight = 220 * px;
    self.tableView.rowHeight = 150 * px;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置滚动条的内边距
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYCHeaderFooterView * head = [WYCHeaderFooterView headFootViewTableView:tableView section:section];
    head.delegate = self;
    head.group = self.dataArray[section];
    return head;
}

-(void)reloadTableView
{
    [self.tableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    WYCTransLationModel * head = self.dataArray[section];
    return  head.isOpen ? head.dataArray.count : 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    tableView.tableFooterView = [[UIView alloc] init];
    WYCDTViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[WYCDTViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell" HeightOfRow:50];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    WYCTransLationModel * model = self.dataArray[indexPath.section];
    WYCTransDetailModel * DetailModel = model.dataArray[indexPath.row];
    
    cell.timeLabel.text = DetailModel.billDade;
    cell.repaymentLbel.text = DetailModel.comment;
    cell.moneyLabel.text = [NSString stringWithFormat:@"%.2f",[DetailModel.amount doubleValue]];
    return cell;
}

@end
